﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Application.Services;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 用户管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("system.account")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }




        /// <summary>
        /// 获取用户分页列表
        /// </summary>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>用户分页列表</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<UserDto>>> GetPagedList([FromBody] UserPageRequest pageRequest)
        {
            var result = await _userService.GetPagedListAsync(pageRequest);
            return ApiResponse<PagedResult<UserDto>>.Ok(result);
        }

        /// <summary>
        /// 获取用户详情
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>用户详情</returns>
        [HttpPost]
        public async Task<ApiResponse<UserDto>> GetById([FromBody] IdRequest request)
        {
            var user = await _userService.GetByIdAsync(request.Id);
            return ApiResponse<UserDto>.Ok(user);
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="input">创建用户请求参数</param>
        /// <returns>用户详情</returns>
        [HttpPost]
        [RequirePermission("system.account")] // 指定所需的权限
        public async Task<ApiResponse<UserDto>> Create([FromBody] CreateUserDto input)
        {
            var user = await _userService.CreateAsync(input);
            return ApiResponse<UserDto>.Ok(user);
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="input">更新用户请求参数</param>
        /// <returns>用户详情</returns>
        [HttpPost]
        [RequirePermission("system.account")] // 指定所需的权限
        public async Task<ApiResponse<UserDto>> Update([FromBody] UpdateUserDto input)
        {
            var user = await _userService.UpdateAsync(input);
            return ApiResponse<UserDto>.Ok(user);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="request">ID请求参数</param>
        [HttpPost]
        [RequirePermission("system.account")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _userService.DeleteAsync(request.Id);

            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 分配角色
        /// </summary>
        /// <param name="input">分配角色请求参数</param>
        [HttpPost]
        [RequirePermission("system.account")] // 指定所需的权限
        public async Task<ApiResponse<object>> AssignRoles([FromBody] AssignRolesDto input)
        {
            await _userService.AssignRolesAsync(input);
            return ApiResponse<object>.Ok(new object());
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="input">修改密码请求参数</param>
        [HttpPost]
        [RequirePermission("system.account")] // 指定所需的权限
        public async Task<ApiResponse<object>> UpdatePassword([FromBody] UpdatePasswordDto input)
        {
            await _userService.UpdatePasswordAsync(input.UserId, input.NewPassword);
            return ApiResponse<object>.Ok(new object());
        }

    }
}